package inits

import (
	"925/video-srv/basic/config"
	"925/video-srv/model"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var err error

func MysqlInit() {
	config.Once.Do(func() {
		// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
		dsn := "root:123456@tcp(127.0.0.1:3306)/eee?charset=utf8mb4&parseTime=True&loc=Local"
		config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
		if err != nil {
			panic(err)
		}
		fmt.Println("mysql连接成功")
		config.DB.AutoMigrate(&model.User{}, &model.Video{}, &model.VideoEp{}, &model.Chap{}, &model.Check{})
		sqlDB, _ := config.DB.DB()
		sqlDB.SetMaxIdleConns(10)
		sqlDB.SetMaxOpenConns(100)
		sqlDB.SetConnMaxLifetime(0)
	})
}
